﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using YArchitech.Revit;
using YJKRevitKernel.CurveUtility;
using YJKRevitKernel.CurveUtility.CurveGroupSuite;

namespace YJKCopyModel.GeothermalPipeQuickModel
{
	[Grouper(GrouperName = "GeothermalGrouper")]
	internal class GeothermalGrouper : IDataGrouper
	{
		public List<RevitModelRequest> GroupData(InputRequest inputRequest)
		{
			List<RevitModelRequest> list = new List<RevitModelRequest>();
			GeothermalRequest geothermalRequest = inputRequest as GeothermalRequest;
			Document document = inputRequest.UseExternalCommandData.Application.ActiveUIDocument.Document;
			Transaction transaction = new Transaction(document, "创建线型");
			try
			{
				transaction.Start();
				ElementId solidPatternId = LinePatternElement.GetSolidPatternId();
				OverrideGraphicSettings overrideGraphicSettings = new OverrideGraphicSettings();
				overrideGraphicSettings.SetProjectionLinePatternId(solidPatternId);
				bool isSame = GeothermalFormData.IsSame;
				if (isSame)
				{
					bool flag = GeothermalFormData.LineTypeSupply == "实线";
					ElementId linePatternElementId;
					if (flag)
					{
						linePatternElementId = solidPatternId;
					}
					else
					{
						LinePatternElement linePatternElement = (from e in new FilteredElementCollector(document).OfClass(typeof(LinePatternElement))
						where e.Name == GeothermalFormData.LineTypeSupply
						select e).ToList<Element>().First<Element>() as LinePatternElement;
						linePatternElementId = linePatternElement.Id;
					}
                    Autodesk.Revit.DB.Color lineColor = ColorSet.GetLineColor(ColorSet.GetXmlDataColor(GeothermalFormData.LineColorSupply));
					GraphicsStyle supplyGraphicsStyle = this.CreateGraphicsStyle(document, linePatternElementId, lineColor, Convert.ToInt32(GeothermalFormData.LineWidthSupply), "地热盘管供水");
					List<ICurveGeter> list2 = geothermalRequest.LstSupplyCurveInfos.ConvertAll<ICurveGeter>((GeothermalCurveIfo thisCurev) => new CurveGeterBean
					{
						UseCurve = thisCurev.cCurve
					});
					for (int i = 0; i < list2.Count; i++)
					{
						Curve useCurve = list2[i].UseCurve;
						bool flag2 = useCurve is Arc;
						if (flag2)
						{
							list2[i].UseCurve = Line.CreateBound(geothermalRequest.LstSupplyCurveInfos[i].ptStart, geothermalRequest.LstSupplyCurveInfos[i].ptEnd);
						}
					}
					Dictionary<Curve, ICurveGeter> dictionary = CurveGroupManger.MakeExtendCurve(list2, 5.0);
					List<List<Curve>> list3 = CurveGroupManger.GroupCurvesByIntersection(dictionary.Keys.ToList<Curve>());
					foreach (List<Curve> list4 in list3)
					{
						GeothermalModelRequest geothermalModelRequest = new GeothermalModelRequest();
						geothermalModelRequest.SupplyGraphicsStyle = supplyGraphicsStyle;
						foreach (Curve curve in list4)
						{
							foreach (GeothermalCurveIfo geothermalCurveIfo in geothermalRequest.LstSupplyCurveInfos)
							{
								GeothermalCurveIfo geothermalCurveIfo2 = geothermalCurveIfo;
								bool flag3 = curve.Distance(geothermalCurveIfo2.ptEnd) < 0.05 && curve.Distance(geothermalCurveIfo2.ptStart) < 0.05;
								if (flag3)
								{
									geothermalModelRequest.LstSupplyCurves.Add(geothermalCurveIfo2.cCurve);
									break;
								}
							}
						}
						list.Add(geothermalModelRequest);
					}
				}
				else
				{
					bool flag4 = GeothermalFormData.LineTypeSupply == "实线";
					ElementId linePatternElementId2;
					if (flag4)
					{
						linePatternElementId2 = solidPatternId;
					}
					else
					{
						LinePatternElement linePatternElement2 = (from e in new FilteredElementCollector(document).OfClass(typeof(LinePatternElement))
						where e.Name == GeothermalFormData.LineTypeSupply
						select e).ToList<Element>().First<Element>() as LinePatternElement;
						linePatternElementId2 = linePatternElement2.Id;
					}
                    Autodesk.Revit.DB.Color lineColor2 = ColorSet.GetLineColor(ColorSet.GetXmlDataColor(GeothermalFormData.LineColorSupply));
					GraphicsStyle supplyGraphicsStyle2 = this.CreateGraphicsStyle(document, linePatternElementId2, lineColor2, Convert.ToInt32(GeothermalFormData.LineWidthSupply), "地热盘管供水");
					bool flag5 = GeothermalFormData.LineTypeBack == "实线";
					ElementId linePatternElementId3;
					if (flag5)
					{
						linePatternElementId3 = solidPatternId;
					}
					else
					{
						LinePatternElement linePatternElement3 = (from e in new FilteredElementCollector(document).OfClass(typeof(LinePatternElement))
						where e.Name == GeothermalFormData.LineTypeBack
						select e).ToList<Element>().First<Element>() as LinePatternElement;
						linePatternElementId3 = linePatternElement3.Id;
					}
                    Autodesk.Revit.DB.Color lineColor3 = ColorSet.GetLineColor(ColorSet.GetXmlDataColor(GeothermalFormData.LineColorBack));
					GraphicsStyle backGraphicsStyle = this.CreateGraphicsStyle(document, linePatternElementId3, lineColor3, Convert.ToInt32(GeothermalFormData.LineWidthBack), "地热盘管回水");
					List<GeothermalCurveIfo> list5 = new List<GeothermalCurveIfo>();
					list5.AddRange(geothermalRequest.LstSupplyCurveInfos);
					list5.AddRange(geothermalRequest.LstBackCurveInfos);
					List<ICurveGeter> list6 = list5.ConvertAll<ICurveGeter>((GeothermalCurveIfo thisCurev) => new CurveGeterBean
					{
						UseCurve = thisCurev.cCurve
					});
					for (int j = 0; j < list6.Count; j++)
					{
						Curve useCurve2 = list6[j].UseCurve;
						bool flag6 = useCurve2 is Arc;
						if (flag6)
						{
							list6[j].UseCurve = Line.CreateBound(useCurve2.GetEndPoint_Cache(0), useCurve2.GetEndPoint_Cache(1));
						}
					}
					Dictionary<Curve, ICurveGeter> dictionary2 = CurveGroupManger.MakeExtendCurve(list6, 5.0);
					List<List<Curve>> list7 = CurveGroupManger.GroupCurvesByIntersection(dictionary2.Keys.ToList<Curve>());
					foreach (List<Curve> list8 in list7)
					{
						GeothermalModelRequest geothermalModelRequest2 = new GeothermalModelRequest();
						geothermalModelRequest2.SupplyGraphicsStyle = supplyGraphicsStyle2;
						geothermalModelRequest2.BackGraphicsStyle = backGraphicsStyle;
						foreach (Curve curve2 in list8)
						{
							foreach (GeothermalCurveIfo geothermalCurveIfo3 in geothermalRequest.LstSupplyCurveInfos)
							{
								GeothermalCurveIfo geothermalCurveIfo4 = geothermalCurveIfo3;
								bool flag7 = curve2.Distance(geothermalCurveIfo4.ptEnd) < 0.05 && curve2.Distance(geothermalCurveIfo4.ptStart) < 0.05;
								if (flag7)
								{
									geothermalModelRequest2.LstSupplyCurves.Add(geothermalCurveIfo4.cCurve);
									break;
								}
							}
							foreach (GeothermalCurveIfo geothermalCurveIfo5 in geothermalRequest.LstBackCurveInfos)
							{
								GeothermalCurveIfo geothermalCurveIfo6 = geothermalCurveIfo5;
								bool flag8 = curve2.Distance(geothermalCurveIfo6.ptEnd) < 0.05 && curve2.Distance(geothermalCurveIfo6.ptStart) < 0.05;
								if (flag8)
								{
									geothermalModelRequest2.LstBackCurves.Add(geothermalCurveIfo6.cCurve);
									break;
								}
							}
						}
						list.Add(geothermalModelRequest2);
					}
				}
				transaction.Commit();
			}
			catch (Exception)
			{
				bool flag9 = transaction.HasStarted();
				if (flag9)
				{
					transaction.RollBack();
				}
			}
			return list;
		}

        public GraphicsStyle CreateGraphicsStyle(Autodesk.Revit.DB.Document doc, ElementId linePatternElementId, Autodesk.Revit.DB.Color color, int width, string strName)
		{
			YJKRevitTools.ModifyLineCategory(doc, strName, linePatternElementId, width, color);
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc).OfClass(typeof(GraphicsStyle));
			foreach (Element element in filteredElementCollector)
			{
				GraphicsStyle graphicsStyle = element as GraphicsStyle;
				bool flag = graphicsStyle == null;
				if (!flag)
				{
					bool flag2 = graphicsStyle.Name.CompareTo(strName) == 0;
					if (flag2)
					{
						return graphicsStyle;
					}
				}
			}
			return null;
		}
	}
}
